package com.chen.controller.user;

import com.chen.common.result.PageResult;
import com.chen.common.result.Result;
import com.chen.judge.codesandbox.model.JudgeResult;
import com.chen.popj.dto.ProblemPageQueryDTO;
import com.chen.popj.dto.ProblemSubmitDTO;
import com.chen.popj.vo.ProblemListVO;
import com.chen.popj.vo.ProblemVO;
import com.chen.service.ProblemService;
import com.chen.service.impl.ElasticSearchServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController(value = "userProblemController")
@RequestMapping("/user")
@Slf4j
public class ProblemController {

    @Autowired
    ProblemService problemService;

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Autowired
    ElasticSearchServiceImpl elasticSearchComponent;

    /**
     * 题目分页查询
     *
     * @param problemPageQueryDTO
     * @return
     */
    @PostMapping("/problemList")
    public Result<PageResult> page(@RequestBody ProblemPageQueryDTO problemPageQueryDTO) {
//        PageResult pageResult = problemService.pageQuery(problemPageQueryDTO);
        PageResult<ProblemListVO> pageResult = new PageResult<>();
        int integer = -1;
        try {
            integer = Integer.parseInt(problemPageQueryDTO.getTag());
            if (integer < 0)
                return Result.error("题目编号应该是大于0的正整数");
            pageResult = elasticSearchComponent.queryProblemListVOById("problemlist", problemPageQueryDTO);
            return Result.success(pageResult);

        } catch (NumberFormatException e) {
            System.out.println(problemPageQueryDTO.getTag() + "不是整数");
        }
        pageResult = elasticSearchComponent.queryProblemListVOByTitle("problemlist", problemPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据题目id查询题目详情
     *
     * @param problemId
     * @return
     */
    @GetMapping("/problemDetails")
    public Result<ProblemVO> queryProblem(Integer problemId) {
        log.info("题目id:{}", problemId);
        ProblemVO problem = problemService.queryProblem(problemId);
        return Result.success(problem);
    }

    /**
     * 提交题目代码
     *
     * @param problemSubmitDTO
     * @return
     */
    @PostMapping("/problemSubmit")
    public Result<Object> problemSubmit(@RequestBody ProblemSubmitDTO problemSubmitDTO) {
//        System.out.println(problemSubmitDTO);
        Integer problemSubmitId = problemService.problemSubmit(problemSubmitDTO, null);
        return Result.success(problemSubmitId);
    }

    /**
     * 根据提交id获取题目做题结果
     *
     * @param problemSubmitId
     * @return
     */
    @GetMapping("/problemResult")
    public Result<JudgeResult> getProblemResult(Integer problemSubmitId) {
        log.info("提交id:{}", problemSubmitId);
        JudgeResult judgeResult = problemService.getProblemResult(problemSubmitId);
        System.out.println(judgeResult);
        return Result.success(judgeResult);
    }
}
